#include <stdio.h>
#include <stdlib.h>

#include <GL3/gl3w.h>
//#include <GL3/gl3.h>
#include <GL/glu.h>

#include <SDL/SDL.h>
//#include <SDL/SDL_opengl.h>
#include <SDL/SDL_image.h>

GLuint loadTexture(const char* path) {
	GLuint texture;

	glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );

	glGenTextures( 1, &texture );
	glBindTexture( GL_TEXTURE_2D, texture );

	SDL_Surface* surface = IMG_Load( path );
	
	SDL_PixelFormat *format = surface->format;
	
	if( format->Amask ) {
		gluBuild2DMipmaps( GL_TEXTURE_2D, 4, surface->w, surface->h, GL_RGBA,GL_UNSIGNED_BYTE, surface->pixels );
	} else {
		gluBuild2DMipmaps( GL_TEXTURE_2D, 3, surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels );
	}
	SDL_FreeSurface( surface );

	// Nice trilinear filtering.
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); 
	glGenerateMipmap( GL_TEXTURE_2D );

	return texture;
}
